package com.issac.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.issac.mmall.entity.OrderDetail;
import com.issac.mmall.entity.Orders;
import com.issac.mmall.entity.User;
import com.issac.mmall.service.CartService;
import com.issac.mmall.service.OrderDetailService;
import com.issac.mmall.service.OrdersService;
import com.issac.mmall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Issac
 * @since 2020-12-10
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CartService cartService;

    @PostMapping("/settlement3")
    public ModelAndView settlement3(Orders orders, HttpSession session, String address, String remark){
        ModelAndView modelAndView = new ModelAndView();
        User user = (User) session.getAttribute("user");
        ordersService.save(orders, user, address, remark);
        modelAndView.setViewName("settlement3");
        if(user == null){
            modelAndView.addObject("cartList",new ArrayList<>());
        }else {
            modelAndView.addObject("orders",orders);
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }
    @GetMapping("/orderList")
    public ModelAndView orderList(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        User user = (User) session.getAttribute("user");
        modelAndView.setViewName("orderList");
        if(user == null){
            modelAndView.addObject("cartList",new ArrayList<>());
        }else {
            modelAndView.addObject("orderProductVOList",ordersService.findAllOrderProductVOByUserId(user.getId()));
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

}

